package pers.qtong.tank;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TankFrame extends Frame {
    static final int GAME_WIDTH = PropertiesMgr.getInstance().getInt("gameWidth");
    static final int GAME_HEIGHT = PropertiesMgr.getInstance().getInt("gameHeight");
//    static final int GAME_WIDTH = 1000;
//    static final int GAME_HEIGHT = 800;

    public GameModel gm = GameModel.getInstance();

//    Explode explode = new Explode(100, 100, this);

    public TankFrame() {
        setSize(GAME_WIDTH, GAME_HEIGHT);
        setTitle("坦克大战");    //标题名字
        setVisible(true);     //设置窗口能否看见
        setResizable(false);   //设置窗口能否改变大小
        addKeyListener(new MyKeyLisTenner());   //增加键盘监听器
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0); //可以关闭
            }
        });
    }

    //解决闪烁
    Image offScreenImage = null;
    @Override
    public void update(Graphics g) {
        if(offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    @Override
    public void paint(Graphics g) { //画笔
        Color color = g.getColor();
        g.setColor(Color.white);
        g.drawString("箭头控制移动", 10, 60);
        g.drawString("ctrl发射子弹", 10, 80);
        g.drawString("S存档", 10, 100);
        g.drawString("L读档", 10, 120);
        g.setColor(color);
        gm.paint(g);
    }

    //键盘监听器
    class MyKeyLisTenner extends KeyAdapter {

        boolean bL = false;
        boolean bR = false;
        boolean bU = false;
        boolean bD = false;

        @Override   //按键压下
        public void keyPressed(KeyEvent e) {
            int k = e.getKeyCode();
            switch (k) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
                default:
                    break;
            }
            //坦克移动
            setTankMainDir();
        }

        @Override   //按键松开
        public void keyReleased(KeyEvent e) {
            int k = e.getKeyCode();
            switch (k) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                case KeyEvent.VK_CONTROL:
                    gm.myTank.fire();  //开火
                    break;
                case KeyEvent.VK_S:
                    gm.save();
                    break;
                case KeyEvent.VK_L:
                    gm.load();
                    break;
                case KeyEvent.VK_SPACE:
                    gm.stop();
                    break;
                default:
                    break;
            }
            //坦克移动
            setTankMainDir();
        }


        private void setTankMainDir() {
            Tank myTank = GameModel.getInstance().getMainTank();
            if(!bL && !bR && !bU && !bD) {
                myTank.setMoving(false);
            }else {
                myTank.setMoving(true);
                if(bU) myTank.setDir(Dir.UP);
                if(bD) myTank.setDir(Dir.DOWN);
                if(bL) myTank.setDir(Dir.LEFT);
                if(bR) myTank.setDir(Dir.RIGHT);
            }
        }

    }
}
